<template>
  <div class="app-container">
    <!-- 搜索区域 -->
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="90px" class="query-form">
      <el-form-item label="行政区划" prop="area">
        <el-select v-model="queryParams.area" placeholder="请选择行政区划" clearable size="small">
          <el-option v-for="item in regionList" :key="item.code" :label="item.name" :value="item.code"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="敏感点名称" prop="pointName">
        <el-input
          v-model="queryParams.pointName"
          placeholder="请输入敏感点名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="敏感点类型" prop="pointType">
        <el-select v-model="queryParams.pointType" placeholder="请选择数据类型" clearable size="small">
          <el-option v-for="item in pointTypeOptions" :label="item.label" :value="item.Value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="info" icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作按钮区域 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
    </el-row>

    <!-- 表格数据区域 -->
    <el-table v-loading="loading" :data="pointList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" width="50" align="center" />
      <el-table-column label="敏感点名称" align="center" prop="pointName" :show-overflow-tooltip="true" />
      <el-table-column label="敏感点类型" align="center" prop="pointType">
        <template slot-scope="scope">
          <template v-for="item in pointTypeOptions">
            <span v-if="item.value == scope.row.pointType">{{item.label}}</span>
          </template>
        </template>
      </el-table-column>
      <el-table-column label="行政区划" align="center" prop="area" :show-overflow-tooltip="true" >
        <template slot-scope="scope">
          <template v-for="item in regionList">
            <span v-if="item.code == scope.row.area">{{item.name}}</span>
          </template>
        </template>
      </el-table-column>
      <el-table-column label="地址" align="center" prop="address" :show-overflow-tooltip="true" />
      <el-table-column label="经度" align="center" prop="longitude" />
      <el-table-column label="纬度" align="center" prop="latitude" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="pageNum"
      :limit.sync="pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改敏感点对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="敏感点名称" prop="pointName">
              <el-input v-model="form.pointName" placeholder="请输入" />
            </el-form-item>
          </el-col>

        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="地址" prop="address">
              <el-input v-model="form.address" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="行政区划" prop="area">
              <el-select v-model="form.area" placeholder="请选择" clearable style="width: 100%;">
                <el-option v-for="item in regionList" :key="item.code" :label="item.name" :value="item.code"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="敏感点类型" prop="pointType">
              <el-select v-model="form.pointType" placeholder="请选择" clearable style="width: 100%;">
                <el-option v-for="item in pointTypeOptions" :label="item.label" :value="item.Value" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="经度" prop="longitude">
              <el-input v-model="form.longitude" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="纬度" prop="latitude">
              <el-input v-model="form.latitude" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input
                v-model="form.remark"
                type="textarea"
                :rows="4"
                placeholder="请输入"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确定</el-button>
        <el-button @click="cancel">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPoint, getPoint, delPoint, addPoint, updatePoint } from "./api";
import { getRegionList } from '@/api/dataResources/monitorData/region.js'

export default {
  name: "SensitivePoint",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 当前页数
      pageNum: 1,
      // 每页显示条数
      pageSize: 10,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      regionList:[],
      // 点位类型选项
      pointTypeOptions: [
        { value: "1", label: "学校、政府、医院、住宅" },
        { value: "2", label: "自然保护区" },
        { value: "3", label: "重要湿地" },
        { value: "4", label: "其他敏感点" }
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        pointName: '',
        pointType: '',
        area:''
      },
      // 表单参数
      form: {},
      // 表格数据
      pointList: [],
      // 表单校验
      rules: {
        pointName: [
          { required: true, message: "点位名称不能为空", trigger: "blur" }
        ],
        pointType: [
          { required: true, message: "点位类型不能为空", trigger: "change" }
        ],
        longitude: [
          { required: true, message: "经度不能为空", trigger: "blur" }
        ],
        latitude: [
          { required: true, message: "纬度不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getRegionList();
    this.getList();
  },
  methods: {
    async getRegionList() {
      let res = await getRegionList();
      if (res.code == 200) {
        this.regionList = res.rows;
      }
    },
    /** 查询敏感点列表 */
    getList() {
      this.loading = true;
      listPoint(this.queryParams).then(response => {
        this.pointList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.pointId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加敏感点";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const pointId = row.pointId || this.ids
      getPoint(pointId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改敏感点";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.pointId != null) {
            updatePoint(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPoint(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const pointIds = row.pointId || this.ids;
      this.$confirm('是否确认删除敏感点编号为"' + pointIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delPoint(pointIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('sensitivePoint/export', {
        ...this.queryParams
      }, `sensitivePoint_${new Date().getTime()}.xlsx`)
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        pointId: null,
        pointName: null,
        pointType: null,
        longitude: null,
        latitude: null,
        area: null,
        address: null,
        contact: null,
        phone: null,
        createTime: null
      };
      this.resetForm("form");
    }
  }
};
</script>

<style lang="scss" scoped>
.app-container {
  padding: 20px;
}

.query-form {
  margin-bottom: 15px;
}

.mb8 {
  margin-bottom: 8px;
}

.small-padding {
  padding: 8px;
}

.fixed-width {
  width: 150px;
}

.dialog-footer {
  text-align: right;
  padding: 10px 20px;
  box-sizing: border-box;
}
</style>